package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Balance;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Kanade on 2018/5/21 0021.
 */

public class BalanceAdapter extends RecyclerView.Adapter<BalanceAdapter.ViewHolder> {
    private List<Balance.RecordsBean> mBalanceList = new ArrayList<>();
    private Context mContext;

    public BalanceAdapter(List<Balance.RecordsBean> balanceList) {
        this.mBalanceList = balanceList;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_description)
        TextView tvDescription;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_amount)
        TextView tvAmount;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_balance_detail, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Balance.RecordsBean balanceRecord = mBalanceList.get(position);
        LogUtil.d("balanceRecord: " + balanceRecord.toString());
        holder.tvDescription.setText(balanceRecord.getFromType());
        holder.tvTime.setText(TimeUtil.getDateToString(balanceRecord.getCreateDate()));
        if (TextUtils.equals(balanceRecord.getType(), "1")) {
            holder.tvAmount.setText("+" + balanceRecord.getAmount());
        } else {
            holder.tvAmount.setText("-" + balanceRecord.getAmount());
        }
    }

    @Override
    public int getItemCount() {
        return mBalanceList.size();
    }

    public void setData(List<Balance.RecordsBean> list) {
        LogUtil.i("setData: " + list.toString());
        this.mBalanceList.clear();
        this.mBalanceList.addAll(list);
        notifyDataSetChanged();
    }
}
